﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Smile.Mvvm.Model
{
    public class ModelBase : INotifyPropertyChanged, INotifyDataErrorInfo
    {
        // @Events
        public event PropertyChangedEventHandler                PropertyChanged;
        public event EventHandler<DataErrorsChangedEventArgs>   ErrorsChanged;


        // @Properties
        public bool HasErrors
        {
            get 
            {
                foreach (var error in _errors)
                {
                    if (error.Value != null && error.Value.Count > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        public string FirstError
        {
            get
            {
                foreach (var error in _errors)
                {
                    if (error.Value != null && error.Value.Count > 0)
                    {
                        return error.Value[0];
                    }
                }
                return string.Empty;
            }
        }


        // @Public
        public void RaisePropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void RaiseErrorsChanged([CallerMemberName] string propertyName = "")
        {
            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
            }
        }

        public IEnumerable GetErrors(string propertyName)
        {
            if (String.IsNullOrEmpty(propertyName) ||
                !_errors.ContainsKey(propertyName))
            {
                return null;
            }
            return _errors[propertyName];
        }

        public void AddError(string propertyName, string error)
        {
            if (!_errors.ContainsKey(propertyName))
            {
                _errors[propertyName] = new List<string>();
            }

            if (!_errors[propertyName].Contains(error))
            {
                _errors[propertyName].Add(error);
                RaiseErrorsChanged(propertyName);
            }
        }

        public void RemoveError(string propertyName, string error)
        {
            if (_errors.ContainsKey(propertyName) &&
                _errors[propertyName].Contains(error))
            {
                _errors[propertyName].Remove(error);
                RaiseErrorsChanged(propertyName);
            }

        }

        public void ClearErrors([CallerMemberName] string propertyName = "")
        {
            if (_errors.ContainsKey(propertyName))
            {
                _errors[propertyName].Clear();
                RaiseErrorsChanged(propertyName);
            }
        }

        public void ReinitializeErrors()
        {
            _errors = new Dictionary<string, List<string>>();
            RaiseErrorsChanged("");
        }


        // @Protected
        protected Dictionary<string, List<string>>  _errors = new Dictionary<string, List<string>>();
    }
}
